<?php

namespace ApiBundle\Controller\Admin;

use ApiBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class FileController extends BaseController
{
    /*
     * 添加文件分类
     */
    public function addFileCategoryAction(Request $request)
    {
        $conn= $this->get("database_connection");
        $title = $request->get("title");

        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }
        if(empty($title)){
            return $this->jsonResponse(1,"参数错误");
        }

        $data = $conn->fetchAssoc("SELECT * FROM file_category WHERE admin_id = '". $this->getAdminId() ."' AND name = '". $title ."'");

        if($data){
            return $this->jsonResponse(1,"该文件夹已存在");
        }

        $conn->insert("file_category",array("name"=>$title,"admin_id"=>$this->getAdminId(),"district_id"=>$this->getDistrictId()));


        return $this->jsonResponse(0,"操作成功");

    }

    /*
     * 获取文件分类列表
     */
    public function getFileCategoryAction()
    {

        $conn = $this->get("database_connection");
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $admin_role_id = $this->get("session")->get("admin_role_id");

        if($admin_role_id == 4){

            $sql = "SELECT * FROM file_category WHERE admin_id = '". $this->getAdminId() ."'";
        } else {

            $sql = "SELECT * FROM file_category WHERE district_id = '". $this->getDistrictId() ."'";
        }

        $data = $conn->fetchAll($sql);
        return $this->jsonResponse(0,"操作成功",array("info" => $data));
    }


    /*
     * 获取文件列表  category_id
     */
    public function getFileListAction(Request $request)
    {
        $category_id = $request->get("id");
        $conn = $this->get("database_connection");

        $page = $request->get("page","1");

        $rows = $request->get("rows","10");


        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        if(empty($category_id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $sql = "SELECT * FROM file WHERE file_category_id = '". $category_id ."'";


        // TODO 搜索
        $search = $request->get("search_keyword");
        if(!empty($search)){

            $sql .= " AND (title LIKE '%". $search ."%' OR keyword LIKE '%". $search ."%') ";
        }

        // 按发布时间搜索
        $start_time = $request->get("start_time");
        $end_time = $request->get("end_time");
        if(!empty($start_time) && !empty($end_time)){
            $sql .= " AND DATE_FORMAT(created_at,'%Y-%m-%d') BETWEEN '". $start_time ."' AND '". $end_time ."'  ";
        }

        $sql .= " ORDER BY created_at DESC ";

        // 带搜索条件下的总数
        $search_all_count = count($conn->fetchAll($sql));
        $all_page = ceil($search_all_count / $rows);

        // 分页
        $m = ($page -1) * $rows;

        $sql .= " LIMIT $m,$rows ";

        $data = $conn->fetchAll($sql);


        return $this->jsonResponse(0,"操作成功",array("info" => $data,"search_all_count"=>$search_all_count,"all_page"=>$all_page));
    }

    /*
     * 添加/编辑文件
     */
    public function editFileAction(Request $request)
    {
        $conn = $this->get("database_connection");

        $id = $request->get("id");
        $title = $request->get("title");
        $keyword = $request->get("keyword");
        $summary = $request->get("summary");
        $attachment_id = $request->get("attachment_id");
        $is_public = $request->get("is_public");
        $file_category_id = $request->get("file_category_id");
        $user_ids = $request->get("user_ids","[]");

        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }


        if(empty($title) || empty($keyword) || empty($summary) || empty($attachment_id) || empty($file_category_id)){
            return $this->jsonResponse(1,"参数错误");
        }

        if($is_public == "1" && empty($user_ids)){
            return $this->jsonResponse(1,"参数错误");
        }

        $user_ids_arrs = json_decode($user_ids);

        
        // TODO: file表添加数据
        $data = array(
            "file_category_id" => $file_category_id,
            "attachment_id" => $attachment_id,
            "title" => $title,
            "keyword" => $keyword,
            "summary" => $summary,
            "is_public" => $is_public,
        );

        if($id == "0" || $id == ""){
            $data['created_at'] = date("Y-m-d H:i:s");
            $conn->insert("file",$data);
            $new_file_id = $conn->lastInsertId();
        } else if($id > "0"){
            $data['updated_at'] = date("Y-m-d H:i:s");
            $where = array(
                "id" => $id
            );
            $conn->update("file",$data,$where);
            $new_file_id = $id;
        }


        // TODO :  user_file 表添加数据
        if($is_public == "1"){

            // user_file表删除旧数据
            $conn->delete("user_file",array("file_id"=> $new_file_id));

            foreach($user_ids_arrs as $item){

                $if_has = $conn->fetchAssoc("SELECT * FROM user_file WHERE user_id = '". $item ."' AND file_id = '".$new_file_id  ."'");
                if(!$if_has){
                    $user_file = array(
                        "user_id" => $item,
                        "file_id" => $new_file_id,
                        "created_at" => date("Y-m-d H:i:s")
                    );
                    $conn->insert("user_file",$user_file);
                }

            }
        }

        return $this->jsonResponse(0,"操作成功");
    }

    /*
     * 获取文件详情
     */
    public function getFileDetailAction(Request $request)
    {
        $conn = $this->get("database_connection");
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $id = $request->get("id");
        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $data = $conn->fetchAssoc("SELECT f.*, a.origin_name as attachment_name FROM file f LEFT JOIN attachment a ON f.attachment_id = a.id WHERE f.id = '". $id ."' ");
        return $this->jsonResponse(0,"操作成功",array("info" => $data));
    }


    /*
     * 删除文件
     */
    public function deleteFileAction(Request $request)
    {
        $conn = $this->get("database_connection");
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $id = $request->get("id");
        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $conn->delete("file",array("id" => $id));
        return $this->jsonResponse(0,"操作成功");
    }
}
